package com.zhdl.network.service.impl;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.project.command.instruction.bo.SingleInstruction;
import com.zhdl.common.util.IdUtil;
import com.zhdl.network.modbus.after.ModbusAfterService;
import com.zhdl.network.modbus.analysis.ModbusAnalysis;
import com.zhdl.network.service.LaserRangingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.project.command.instruction.strategy.abs.AbsDic.INSTRUCTION_DEFAULT_HEX;

@Slf4j
@Service
public class LaserRangingServiceImpl implements LaserRangingService {

    @Resource
    private BehaviorPerform behaviorPerform;

    /**
     * 发送指令 - 灵活
     *
     * @param cmd 指令
     */
    @Override
    public void sendCmd(String cmd) {
        SingleInstruction instruction = new SingleInstruction();
        instruction.setInstructionId(IdUtil.getUUID_8());
        instruction.setInstruction(cmd);
        instruction.setBeforeInstructionName(INSTRUCTION_DEFAULT_HEX);
        //默认使用com.zhdl.instruction.strategy包下后置策略
        instruction.setAfterInstructionName(ModbusAfterService.MODBUS_AFTER_SERVICE);
        //客户端通信地址, 串口为数字, 网口为ip+端口       数据来源 : port_comm 或 port_socket 表
        instruction.setPortKey("2");
        //设置协议  数据来源 : protocols 表
        instruction.setProtocols(ModbusAnalysis.MODBUS_ANALYSIS);
        behaviorPerform.perform(instruction);
    }


    /**
     * 测距-发送单次测距
     * 0x01 0x03 0x04 0x00 0x00 0xE0 0xA1 0x72 0x4B
     * E0A1 -> 57505mm
     */
    @Override
    public void sendRangingOnce() {
        String cmd = "0103000F0002F408";
        sendCmd(cmd);
    }

}
